# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import json
from common.config import *


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "千万综合交通意外险"

    def selectData(self, data={}):
        SQL = "SELECT * FROM `" + self.name + "` WHERE state = 2 "

        self.mysql.cursor.execute(SQL)

        result = self.mysql.cursor.fetchall()

        for item in result:
            result = {'1-17岁': int(item["interval_zero"]), '18-80岁': int(item["interval_eighteen"])}

            data["name"] = self.name
            data["company"] = "安盛保险"
            data["days"] = item["day"] + "d"
            data["people_count"] = "'" + json.dumps(result, ensure_ascii=False) + "'"
            data["airplane_death"] = "10000000"
            data["airplane_disability"] = "100000"
            data["train_death"] = "1000000"
            data["train_disability"] = "100000"
            data["ship_disability"] = "100000"
            data["price"] = item["price"]
            self.mergeTable(data)

    # 合并表格
    def mergeTable(self, data):
        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`airplane_death`,`airplane_disability`,`train_death`,`train_disability`,`ship_disability`,`price`)VALUES("{name}","{company}","{days}",{people_count},"{airplane_death}","{airplane_disability}","{train_death}","{train_disability}","{ship_disability}","{price}")'

        print(SQL.format(**data))
        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()

        print(data)
